package cn.com.mayue.guava.reflect;

import com.google.common.reflect.Reflection;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by mayue
 * ISomething 被 InvocationHandler 代理
 * 动态代理使用
 */
public class DynamicProxyTest {
    public static void main(String[] args) {
        InvocationHandler invocationHandler = new TestInvocationHandler();

        // guava
        ISomething guava = Reflection.newProxy(ISomething.class, invocationHandler);
        guava.doSomething();
        //jdk
        ISomething jdk = (ISomething) Proxy.newProxyInstance(
                ISomething.class.getClassLoader(),
                new Class<?>[]{ISomething.class},
                invocationHandler);
        jdk.doSomething();
    }

    public static class TestInvocationHandler implements InvocationHandler {
        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            System.out.println("proxy。。。。");
            return null;
        }
    }

    public static interface ISomething {
        void doSomething();
    }
}
